package com.longteng.diamond.controller;

import com.longteng.diamond.domain.InterfaceTest;
import com.longteng.diamond.domain.ProjectShareNode;
import com.longteng.diamond.domain.TestCase;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.po.TreeMenu;
import com.longteng.diamond.service.ExpressionCheckService;
import com.longteng.diamond.service.InterfaceTestService;
import com.longteng.diamond.service.TestCaseService;
import com.longteng.diamond.service.impl.ProjectHostInterface;
import com.longteng.diamond.service.impl.ProjectModuleInterface;
import com.longteng.diamond.util.SessionUtil;
import com.longteng.diamond.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/6/4
 * TIME : 23:07
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Slf4j
@Controller
@SessionAttributes(value = {"interfaceId", "interfaceMethod"})
@RequestMapping("/testCaseController")
public class TestCaseController {
    @Autowired
    TestCaseService testCaseService;
    @Autowired
    InterfaceTestService interfaceTestService;
    @Autowired
    ProjectModuleInterface projectModuleInterface;
    @Autowired
    ValidUtil validUtil;
    @Autowired
    ExpressionCheckService expressionCheckService;
    /**
     * 跳转测试用例页面
     *
     * @param
     * @return
     */
    @RequestMapping("/testPage")
    public ModelAndView interfaceTestPage(Integer interfaceId, String interfaceMethod, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (null != interfaceId || null != interfaceMethod) {
            Integer sessionInterfaceId = (Integer) session.getAttribute("interfaceId");
            if (null == sessionInterfaceId || !interfaceId.equals(sessionInterfaceId)) {
                modelAndView.addObject("interfaceId", interfaceId);
            }
            String sessionInterfaceMethod = (String) session.getAttribute("interfaceMethod");
            if (null == sessionInterfaceMethod || !interfaceMethod.equals(sessionInterfaceMethod)) {
                modelAndView.addObject("interfaceMethod", interfaceMethod);
            }
        }
        modelAndView.setViewName("page/testcase/testCasePage");
        return modelAndView;
    }

    /**
     * 获取所有测试用例
     *
     * @param testCase
     * @return tablePage
     */
    @RequestMapping("/getTestCase")
    public @ResponseBody
    TablePage getTestCase(TestCase testCase) {
        TablePage tablePage = new TablePage();
        try {
            tablePage = testCaseService.getTestCase(testCase);
        } catch (Exception e) {
            log.error("获取用户错误:", e);
        }
        return tablePage;
    }

    /**
     * 新增测试用例
     *
     * @param testCase
     * @return
     */
    @RequestMapping("/addTestCase")
    public ModelAndView addTestCase(@Valid TestCase testCase, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        /**通过jsr303校验进行参数基本校验**/
        if (!validUtil.checkParam(testCase.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/testcase/editTestCasePage");
            return modelAndView;
        }
        try {
            Integer projectId = SessionUtil.getSessionUser().getLastProjectId();
            testCase.setProjectId(projectId);
            testCaseService.addTestCase(testCase);
        } catch (Exception e) {
            log.error("新增测试用例错误", e);
            modelAndView.setViewName("/testCaseController/editTestCasePage");
            modelAndView.addObject("errorMsg", e.toString());
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/testCaseController/testPage");
        return modelAndView;
    }

    /**
     * 更新测试用例
     *
     * @param testCase
     * @return
     */
    @RequestMapping("/updateTestCase")
    public ModelAndView updateTestCase(@Valid TestCase testCase, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(testCase.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/testcase/editTestCasePage");
            return modelAndView;
        }
        try {
            testCaseService.updateTestCase(testCase);
        } catch (Exception e) {
            log.error("更新接口错误", e);
        }
        modelAndView.setViewName("redirect:/testCaseController/testPage");
        return modelAndView;
    }

    /**
     * 新增编辑测试用例页面
     *
     * @param testCase
     * @return
     */
    @RequestMapping("/editTestCasePage")
    public ModelAndView editTestCase(TestCase testCase) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/testcase/editTestCasePage");
        if (null != testCase.getTestCaseId() && testCase.getTestCaseId() > 0) {
            testCase = testCaseService.findTestCaseById(testCase.getTestCaseId());
        }
        modelAndView.addObject("testCase", testCase);
        return modelAndView;
    }

    /**
     * 删除测试用例
     *
     * @param testCaseId
     * @return
     */
    @RequestMapping("/deleteTestCase")
    public @ResponseBody
    RequestResult deleteTestCase(Integer testCaseId) {
        RequestResult requestResult = new RequestResult();
        try {
            testCaseService.deleteTestCaseById(testCaseId);
            requestResult.setSuccess(true);
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("删除测试用例异常,请联系管理员");
            log.error("删除测试用例异常", e);
        }
        return requestResult;
    }

    /**
     * 测试用例批量绑定接口
     *
     * @param interfaceId
     * @param caseIds
     * @return
     */
    @RequestMapping("/bindInterface")
    public @ResponseBody
    RequestResult bindInterface(Integer interfaceId, Integer[] caseIds) {
        RequestResult requestResult = new RequestResult();
        try {
            testCaseService.bindInterface(interfaceId, caseIds);
            requestResult.setSuccess(true);
            requestResult.setMsg("绑定接口成功");
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("绑定接口错误,请联系管理员!");
            log.error("绑定接口错误", e);
        }
        return requestResult;
    }

    /**
     * 获取用例树,按照接口分组
     *
     * @param projectId
     * @return
     */
    @RequestMapping("/findTestCaseGroupInterface")
    public @ResponseBody
    RequestResult findTestCaseGroupInterface(Integer projectId) {
        RequestResult requestResult = new RequestResult();
        try {
            List<TreeMenu> treeMenuList = testCaseService.findTestCaseGroupInterface(projectId);
            requestResult.setSuccess(true);
            requestResult.setObj(treeMenuList);
        } catch (Exception e) {
            log.error("获取用例树出错", e);
            requestResult.setSuccess(false);
            requestResult.setMsg("获取用例树出错,请联系管理员!");
        }
        return requestResult;
    }

    /**
     * 校验表达式正确性
     *
     * @param
     * @return
     */
    @RequestMapping("/checkExpression")
    public @ResponseBody
    RequestResult checkExpression(String matchExpression,String expectResult,Integer matchType) {
        RequestResult requestResult = null;
        try {
            requestResult=expressionCheckService.check(matchExpression,expectResult,matchType);
        } catch (Exception e) {
            log.error("校验表达式异常", e);
            requestResult = new RequestResult();
            requestResult.setSuccess(false);
            requestResult.setMsg("校验表达式异常,请联系管理员!");
        }
        return requestResult;
    }


}
